<template>
    <div class="app-container">
        <el-form :model="searchForm" inline ref="searchForm" style="position:relative;">
            <el-row>
                <el-col :span="20">
                    <el-form-item label="可用性：" prop="enable">
                        <el-select clearable v-model="searchForm.enable" placeholder="请选择">
                            <el-option v-for="item in usingStatusOpt" :key="item.value" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-button type="primary" @click="init('search')" >查询</el-button>
                </el-col>
                <el-col :span="4">
                    <el-button @click="addClick" type="primary" style="position: absolute;right: 0;">添加规则</el-button>
                </el-col>
            </el-row>
        </el-form>
        <el-table :data="tableData" border style="width: 100%;margin-bottom: 20px;">
            <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
            <el-table-column prop="rulesScore" label="金额" align="center"></el-table-column>
            <el-table-column prop="rulesRatio" label="可得积分数" align="center"></el-table-column>
            <el-table-column prop="enabled" :formatter="usingStatusFun" label="可用性" width="80" align="center"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" align="center" width="160"></el-table-column>
            <el-table-column label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button @click.stop="editClick(scope.row)" type="primary" plain size="mini">编辑</el-button>
                    <el-button @click.stop="statusClick(scope.row)" type="warning" plain size="mini">{{(scope.row.enabled == 1) ? '禁用' : '启用'}}</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @current-change="getList" layout="total, prev, pager, next, jumper" :total="pageData.totalElements"></el-pagination>
        <!-- 弹框 -->
        <el-dialog :title="nowTit" :visible.sync="showDialog" width="800px">
            <el-form ref="dataForm" :rules="rules" :model="dataForm" label-position="left" label-width="100px" style="margin: 0px 50px;">
                <el-form-item label="金额：" prop="rulesScore">
                    <el-input v-model="dataForm.rulesScore" placeholder="必填项。请输入金额"></el-input>
                </el-form-item>
                <el-form-item label="积分数：" prop="rulesRatio">
                    <el-input v-model="dataForm.rulesRatio" placeholder="请输入积分数"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="saveClick('dataForm')" type="primary">保存</el-button>
                    <el-button @click="cancelClick('dataForm')">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
import { addPreferential, findPreferential, getPreferential, editPreferential, enablePreferential, delPreferential } from '@/api/product/preferential-rules'
import { getCookie, setCookie } from '@/utils/auth'
import { validEmail } from '@/utils/validate'
export default {
    name: 'Preferential-rules',
    data() {
        return{
            searchForm: {enable: '', preferentialType: '', rulesNumber: ''},
            tableData: [],
            nowTit: '',
            showDialog: false,
            dataForm: {
                rulesScore: '',
                rulesRatio: '',
                rulesDesc: ''
            },
            rules: {
                rulesScore: [
                    { required: true, message: '请输入金额', trigger: 'blur' }
                ],
                rulesRatio: [
                    { required: true, message: '请输入积分数', trigger: 'blur' }
                ],
            },
            pageData: {},
            nowId: '',
            nowPage: 0,
            pageSize: 10,
            usingStatusOpt: [
                {label: '启用', value: 1},
                {label: '禁用', value: 0}
            ],
        }
    },
    methods: {
        //默认函数
        init(params){
            const _this = this;
            if(params == 'search'){
                _this.nowPage = 0
            }
            //获取列表信息
            findPreferential( _this.nowPage, _this.pageSize, _this.searchForm.enable, _this.searchForm.preferentialType, _this.searchForm.rulesNumber ).then(response => {
                if(response.code == 200){
                    _this.pageData = response.object;
                    _this.tableData = response.object.content;
                }else{
                    this.$message.error(response.message);
                }
            })
        },
        addClick(){
            this.popStatus = 0;
            this.showDialog = true;
            this.dataForm.rulesRatio = '';
            this.dataForm.rulesScore = '';
            if(this.dataForm.id){
                delete this.dataForm.id;
            }
        },
        //编辑
        editClick(row){
            this.popStatus = 1;
            this.nowId = row.id;
            this.dataForm.rulesRatio = row.rulesRatio;
            this.dataForm.rulesScore = row.rulesScore;
            this.showDialog = true;
        },
        //保存
        saveClick(formName){
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //提交
                    if(_this.popStatus == 0){  //添加
                        addPreferential(_this.dataForm).then(response => {
                            if(response.code == 200){
                                this.$message.success(response.message);
                                _this.init();
                                _this.showDialog = false;
                            }else{
                                this.$message.error(response.message);
                            }
                        })

                    }else if(_this.popStatus == 1){  //修改
                        _this.dataForm.id = _this.nowId;
                        editPreferential(_this.dataForm).then(response => {
                            if(response.code == 200){
                                this.$message.success(response.message);
                                _this.init();
                                _this.showDialog = false;
                            }else{
                                this.$message.error(response.message);
                            }
                        })
                    }
                    
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //取消
        cancelClick(formName){
            this.showDialog = false;
            this.$refs[formName].resetFields();
        },
        statusClick(row){
            let usingStatus,isUsing;
            if(row.enabled == 1){  //启用状态
                usingStatus = 0;
                isUsing = '禁用';
            }else{
                usingStatus = 1;
                isUsing = '启用';
            }

            let _this = this;
            this.$confirm('确定修改状态为 '+ isUsing +' 吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //启用禁用
                enablePreferential({id: row.id, enabled: usingStatus}).then(response => {
                    if(response.code == 200){
                        this.$message.success(response.message);
                        _this.init();
                    }else{
                        this.$message.error(response.message);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });          
            });
        },
        usingStatusFun(row){
            if(row.enabled == 1){
                return '启用'
            }else if(row.enabled == 0){
                return '禁用'
            }
        },
        //分页
        getList(val) {
            this.nowPage = val - 1;
            this.init();
        },
    },
    mounted(){
        this.init()
    }
}
</script>
